package com.cx.mall.goods.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.cx.mall.common.model.goods.SpuSaleAttr;
import com.cx.mall.common.model.goods.SpuSaleAttrValue;
import com.cx.mall.goods.mapper.SpuSaleAttrMapper;
import com.cx.mall.goods.service.ISpuSaleAttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.mall.goods.service.ISpuSaleAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyt
 * @since 2025-07-10
 */
@Service
public class SpuSaleAttrServiceImpl extends ServiceImpl<SpuSaleAttrMapper, SpuSaleAttr> implements ISpuSaleAttrService {
    @Autowired
    private ISpuSaleAttrValueService spuSaleAttrValueService;
    public Map<String, List<String>> getSpuSaleAttrs(Long spuId){
        // 获取spuid的所有属性
        LambdaQueryChainWrapper<SpuSaleAttr> spuAttrWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        List<SpuSaleAttr> attrs = spuAttrWrapper.eq(SpuSaleAttr::getSpuId, spuId)
                .select(SpuSaleAttr::getBaseSaleAttrId, SpuSaleAttr::getSaleAttrName)
                .list();
        // 获取spuid的所有属性值
        List<SpuSaleAttrValue> values = spuSaleAttrValueService.getSpuSaleAttrValues(spuId);
        // 封装属性
        Map<String,List<String>> attrMaps =new HashMap<>();
        for (SpuSaleAttr attr : attrs) {
            String k = attr.getSaleAttrName();
            List<String> v = new ArrayList<>();
            for (SpuSaleAttrValue value : values) {
                if(attr.getBaseSaleAttrId() == value.getBaseSaleAttrId()){
                    v.add(value.getSaleAttrValueName());
                }
            }
            attrMaps.put(k,v);
        }
        return  attrMaps;
    }

}
